//index.js
var WxSearch = require('../../myPages/wxSearchView/wxSearchView.js');
var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    userinfo: []
  },

  // 搜索栏
  onLoad: function() {
    var that = this;
    WxSearch.init(
      that, // 本页面一个引用
      ["考研", "金融硕士", "高考志愿", "美国留学", "雅思考试"], // 热点搜索推荐，[]表示不使用
      ["考研", "金融硕士", "高考志愿", "美国留学", "雅思考试"], // 搜索匹配，[]表示不使用
      that.mySearchFunction, // 提供一个搜索回调函数
      that.myGobackFunction //提供一个返回回调函数
    );
  },

  // 转发函数,固定部分
  wxSearchInput: WxSearch.wxSearchInput, // 输入变化时的操作
  wxSearchKeyTap: WxSearch.wxSearchKeyTap, // 点击提示或者关键字、历史记录时的操作
  wxSearchDeleteAll: WxSearch.wxSearchDeleteAll, // 删除所有的历史记录
  wxSearchConfirm: WxSearch.wxSearchConfirm, // 搜索函数
  wxSearchClear: WxSearch.wxSearchClear, // 清空函数

  // 搜索回调函数  
  mySearchFunction: function(value) {
    console.log(value)
    // do your job here
    let searchCondition = {
      pageSize: 20,
      pageNum: 1,
      conditionList: [],
      sort: {}
    };
    let condition = {
      operator: 7,
      code: 'searchContent',
      value: value,
      type: 'String'
    }
    searchCondition.conditionList.push(condition)
    console.log(searchCondition)

    let that = this;
    util.request(api.SearchUrl, searchCondition, 'POST', true).then(function(res) {
      if (res.code === 200) {
        console.log(res)
        //设置介绍文字
        res.result.forEach((value, index, arr) => {
          let userEducationExperiencesTXT = "";
          for (let i in value.userEducationExperiences) {
            if (i == value.userEducationExperiences.length - 1) {
              userEducationExperiencesTXT += value.userEducationExperiences[i].college
            } else {
              userEducationExperiencesTXT += value.userEducationExperiences[i].college + " | "
            }
          }
          value.userEducationExperiencesTXT = userEducationExperiencesTXT.substring(0, userEducationExperiencesTXT.length)
          userEducationExperiencesTXT = "";
        })
        that.setData({
          userinfo: res.result
        });
      } else {
        wx.showModal({
          title: '提示',
          content: res.message,
          showCancel: false
        })
      }
    });
  },

  goToTeacher: function (e) {
    if (e.currentTarget.dataset.id != undefined) {
      wx.navigateTo({
        url: '../../myPages/teacher/teacher?teacherId=' + e.currentTarget.dataset.id
      })
    }
  },

  goToTeacherDetail:function(e){
    wx.navigateTo({
      url: '../../myPages/teacherDetail/teacherDetail?teacherId=' + e.currentTarget.dataset.id
    })
  },

  // 返回回调函数
  myGobackFunction: function() {
    // do your job here
    // 跳转
    /* wx.redirectTo({
       url: '../index/index?searchValue=返回'  
     })
     */
    wx.navigateBack()
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    wx.hideShareMenu()
  },

})